﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ListForms;

namespace ListsForm
{
    public partial class FrmEditPosition : FrmBaseEditForm
    {
        public FrmEditPosition(Lists.Position position)
        {
            InitializeComponent();
            if (position == null)
            {
                _EditState = EditState.CreatingNew;
                bindingSource.AddNew();
                this.Text = "New Position";
            }
            else
            {
                _EditState = EditState.Modifying;
                bindingSource.Add(DB.Positions.FirstOrDefault(p => p.PositionID == position.PositionID));
                this.Text = "Edit Position";
            }
        }

        protected override void CheckFullInfo()
        {
            Lists.Position position = (Lists.Position)bindingSource.Current;
            if (string.IsNullOrEmpty(position.PositionNo))
                throw new Exception("PositionID not allow null");
            if (string.IsNullOrEmpty(position.PositionName))
                throw new Exception("Positon name not allow null");
        }

        protected override void SaveAndClose()
        {
            Lists.Position position = (Lists.Position)bindingSource.Current;
            if (_EditState == EditState.CreatingNew)
                DB.Positions.InsertOnSubmit(position);
            DB.SubmitChanges();
            this.Close();
        }
    }
}
